from flask_mail import Message, Mail  # 邮箱消息
from flask import Flask  # Flask框架
import config  # 导入项目配置文件包

app = Flask(__name__)
# 绑定配置文件
app.config.from_object(config)

mail = Mail()
mail.init_app(app)


@app.route("/")
def send_msg():
    url = "2921363911@qq.com"
    info = r'''
            Traceback (most recent call last):
                      File "C:\Users\Administrator\Desktop\live-shopping-service\api\tests.py", line 28, in <module>
                        tts_audio = AudioSegment.from_mp3(tts_audio_path)
                      File "D:\PyCharm Community\Python\lib\site-packages\pydub\audio_segment.py", line 796, in from_mp3
                        return cls.from_file(file, 'mp3', parameters=parameters)
                      File "D:\PyCharm Community\Python\lib\site-packages\pydub\audio_segment.py", line 728, in from_file
                        info = mediainfo_json(orig_file, read_ahead_limit=read_ahead_limit)
                      File "D:\PyCharm Community\Python\lib\site-packages\pydub\utils.py", line 275, in mediainfo_json
                        res = Popen(command, stdin=stdin_parameter, stdout=PIPE, stderr=PIPE)
                      File "D:\PyCharm Community\Python\lib\subprocess.py", line 947, in __init__
                        self._execute_child(args, executable, preexec_fn, close_fds,
                      File "D:\PyCharm Community\Python\lib\subprocess.py", line 1416, in _execute_child
                        hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
                    FileNotFoundError: [WinError 2] 系统找不到指定的文件。
    '''
    html = f'''
        <html>
            <body>
                <h1 style='color:red'>Error Info(报错信息)：</h1>
                <p>
                    {info}
                </p>
            </body>
        </html>
    '''
    message = Message(subject="message-info", recipients=[url], html=html)
    mail.send(message)
    return "发送成功！！"

    
# 关闭Flask应用程序的函数
def shutdown_server():
    func = request.environ.get('werkzeug.server.shutdown')
    if func is None:
        raise RuntimeError('Not running with the Werkzeug server')
    func()


if __name__ == '__main__':
    webbrowser.open('http://127.0.0.1:5000')
    # app.run(debug=True, host='127.0.0.1', port=5000)
    app.run()
    # 调用shutdown_server()来关闭Flask应用程序
    shutdown_server()
